package zsl.run;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import zsl.algo.MergeAlgo;
import zsl.algo.TAAlgo;
import zsl.generator.DefaultWordListGenerator;
import zsl.generator.UniformWordListGenerator;
import zsl.generator.WordListGenerator;
import zsl.output.ExcelWriter;
import zsl.utils.TAInfo;
import zsl.utils.WordCount;
import zsl.utils.WordCountList;

public class RunInfoCollector {
	
	public static void main(String[] args) throws IOException {
		final long SEED = 1000L;
		final int TOPK = 10;
		
		// factors that may influence 
		int[] numOfWordsArray = {1000};
		int[] numOfListsArray = {200};
		
		for (int wordArrayIndex = 0; wordArrayIndex < numOfWordsArray.length; wordArrayIndex++) {
			int dictLen = numOfWordsArray[wordArrayIndex];
			WordListGenerator wg = new UniformWordListGenerator(dictLen, SEED);
			String generatorName = wg.getClass().getName();
			//start loop over numOfListsArray
			for (int listArrayIndex = 0; listArrayIndex < numOfListsArray.length; listArrayIndex++) {
				ExcelWriter writer = new ExcelWriter();
				
				int numOfWords = numOfWordsArray[wordArrayIndex];
				int numOfLists = numOfListsArray[listArrayIndex];
				
				//generate lists of words
				Vector<WordCountList> lists = new Vector<>();
				for (int j = 0; j < numOfLists; j++) {
					lists.add(wg.generateWords(numOfWords));
				}
				int maxMergeNum =(int) Math.ceil(numOfLists/2.0);
				TAInfo taInfo = null;
				boolean recorded = false;
				System.out.println("==============="+numOfLists+"=============");
				for (int j = maxMergeNum; j > 1; j--) {
					if (isValidBestMerge(j, numOfLists)) {
						Vector<WordCountList> mergedList = MergeAlgo.merge(lists, j, null);
						taInfo = new TAInfo();
						ArrayList<WordCount> topKList  = TAAlgo.getTopK(TOPK, mergedList, taInfo, true); 
//						writer.addCountDistributionSheet(""+mergedList.size(), mergedList);
						writer.addTopKDistributionSheet(""+mergedList.size(), mergedList, topKList);
					}
				}
				writer.write2Excel(generatorName.substring(generatorName.lastIndexOf('.')+1, generatorName.length())+"-TopKDistribution-W"+numOfWordsArray[wordArrayIndex]+"L"+numOfListsArray[listArrayIndex]+".xlsx");
			}//end loop over numOfListsArray
			
		}//end loop over numOfWordsArray
	}
	
	private static boolean isValidBestMerge(int mergeNum, int listNumBeforeMerge){
		return Math.ceil(listNumBeforeMerge / (double)mergeNum) != Math.ceil(listNumBeforeMerge / (double)(mergeNum-1));
	}
}
